<template>
  <div>
    <el-form
      :inline="true"
      size="small"
      ref="tableData"
      :model="tableData"
      class="demo-form-inline"
    >
      <el-form-item prop="id">
        <el-input
          v-model="tableData.id"
          clearable
          @clear="getStaffList"
          placeholder="员工ID"
        ></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="tableData.name" placeholder="员工账号"></el-input>
      </el-form-item>
      <el-form-item prop="linkman">
        <el-input v-model="tableData.linkman" placeholder="员工姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="addDialog">新增</el-button>
        <el-button @click="resetting('tableData')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="staffList" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"> </el-table-column>
      <el-table-column prop="username" label="账号"> </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="age" label="年龄"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="salary" label="薪酬"> </el-table-column>
      <el-table-column prop="entryDate" label="入职时间"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="getDialog(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="delStaff(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[7, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="450px">
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input v-model="ruleForm.age"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="mobile">
            <el-input v-model="ruleForm.mobile"></el-input>
          </el-form-item>
          <el-form-item label="薪酬" prop="salary">
            <el-input v-model="ruleForm.salary"></el-input>
          </el-form-item>
          <el-form-item label="入职时间" prop="entryDate">
            <el-date-picker
              v-model="ruleForm.entryDate"
              type="date"
              placeholder="入职时间"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resettingDia('ruleForm')">取 消</el-button>
        <el-button type="primary" v-if="title == '新增员工'" @click="addStaff"
          >确 定</el-button
        >
        <el-button type="primary" v-if="title == '编辑员工'" @click="editStaff"
          >编 辑</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import staffApi from "../../api/staff";
export default {
  data() {
    return {
      tableData: {
        id: null,
        username: "",
        name: "",
      },
      page: 1,
      size: 7,
      total: 0,
      staffList: [],
      ruleForm: {
        username: "",
        name: "",
        age: "",
        mobile: "",
        salary: "",
        entryDate: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入员工账号", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入员工姓名", trigger: "blur" }],
        age: [{ required: true, message: "请输入员工年龄", trigger: "blur" }],
        mobile: [
          { required: true, message: "请输入员工电话", trigger: "blur" },
        ],
        salary: [
          { required: true, message: "请输入员工薪酬", trigger: "blur" },
        ],
        entryDate: [
          { required: true, message: "请输入员工薪酬", trigger: "change" },
        ],
      },
      dialogVisible: false,
      title: "",
    };
  },
  created() {
    this.getStaffList();
  },
  mounted() {},
  methods: {
    async getStaffList() {
      try {
        let res = await staffApi.getStaff(this.page, this.size, this.tableData);
        if (res.data.flag) {
          this.staffList = res.data.data.rows;
          this.total = res.data.data.total;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    handleSizeChange(val) {
      this.size = val;
      this.getStaffList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getStaffList();
    },
    search() {
      this.getStaffList();
    },
    resetting(formName) {
      this.$refs[formName].resetFields();
    },
    addDialog() {
      this.title = "新增员工";
      this.dialogVisible = true;
    },
    async getDialog(id) {
      this.title = "编辑员工";
      this.dialogVisible = true;
      try {
        let res = await staffApi.searchStaff(id);
        if (res.data.flag) {
          this.ruleForm = res.data.data.rows[0];
          this.ruleForm.entryDate = new Date(res.data.data.rows[0].entryDate);
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    resettingDia(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    async addStaff() {
      try {
        let res = await staffApi.addStaff(this.ruleForm);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getStaffList();
          this.$refs["ruleForm"].resetFields();
          this.dialogVisible = false;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    async editStaff() {
      try {
        let res = await staffApi.editStaff(this.ruleForm.id, this.ruleForm);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getStaffList();
          this.$refs["ruleForm"].resetFields();
          this.dialogVisible = false;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    async delStaff(id) {
      try {
        let res = await staffApi.delStaff(id);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getStaffList();
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
  },
};
</script>

<style scoped >
.el-pagination {
  margin-top: 10px;
}
.el-form {
  margin-top: 20px;
}
</style>
